﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.Entity;
using CMN.Common;
using CMN.Framework;
using CMN.BLL;
namespace CMN.UI
{
    /// <summary>
    /// 通知公告
    /// </summary>
    public class Normal_NoticeDisplay
    {
        static Normal_NoticeDisplay()
        {
            BaseTemplateDisplay.OnNVelocityLoadPath += new EventHandler<TemplateArg>(NoticeLoadPath);
            BaseTemplateDisplay.OnAfterBaseDisplay += new EventHandler<TemplateDisplayArg>(NoticeList);
            BaseTemplateDisplay.OnOtherPageDisplay += new EventHandler<TemplateDisplayArg>(NoticeDisplay);
        }

        /// <summary>
        /// 加载公告模板路径
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void NoticeLoadPath(object o, TemplateArg arg)
        {
            if (arg.PageHelper.OtherTypeName == "notice")
            {
                Normal_Notice notice = new Normal_Notice();
                Normal_NoticeEntity noticeEntity = notice.Disp(arg.PageHelper.ContentId);
                if (noticeEntity == null)
                {
                     Utils.Redirect404();
                     return;
                }
                if (noticeEntity.TemplateId == 0)
                {
                    arg.TemplateEntity = MvcWebCacheManager.GetDefaultTemlateEntity();
                }
                else
                {
                    Normal_TemplateListEntity templateeListEntity = MvcWebCacheManager.GetTemlateListEntity(noticeEntity.TemplateId);
                    arg.TemplateEntity = MvcWebCacheManager.GetTemlateEntity(templateeListEntity.TemplateId);
                }
                arg.TemplateEntity.TemplateName = "notice.htm";
            }

        }
        /// <summary>
        /// 公告绑定模板
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void NoticeList(object o, TemplateDisplayArg arg)
        {
           
                Normal_NoticeDisplay noticeDisplay = new Normal_NoticeDisplay();
                arg.NvelocityTemplate.Put("notice", noticeDisplay);
            
           
        }
        /// <summary>
        /// 公告绑定模板
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void NoticeDisplay(object o, TemplateDisplayArg arg)
        {
            if (arg.PageHelper.OtherTypeName == "notice")
            {
                Normal_Notice notice = new Normal_Notice();

                Normal_NoticeEntity noticeEntity = notice.Disp(arg.PageHelper.ContentId);

                if (noticeEntity != null)
                {
                    arg.PageHelper.Title = noticeEntity.Title;


                    arg.NvelocityTemplate.Put("noticeEntity", noticeEntity);
                }
                else
                {
                    noticeEntity = new Normal_NoticeEntity();
                    arg.PageHelper.Title = noticeEntity.Title;
                    arg.NvelocityTemplate.Put("noticeEntity", noticeEntity);
                    AppLog.Write("NoticeDisplay:" + arg.PageHelper.ContentId + "为空 ", AppLog.LogMessageType.Debug);
                }
            }
        }
        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public List<Normal_NoticeEntity> List(int showNum,int cacheTime)
        {
            QueryParam qp=new QueryParam();
            qp.PageSize=showNum;
            qp.PageIndex=1;
            qp.Orderfld="AddDate desc ";
            BLLHelper bll = new BLLHelper();
            qp.Where=string.Concat("enabled=1 and  startdate<=",bll.GetSqlDateTime(DateTime.Now)," and enddate>= ",bll.GetSqlDateTime(DateTime.Now) );
            List<Normal_NoticeEntity> noticeList = MvcWebCacheManager.GetNormal_NoticeList(qp, cacheTime);
            foreach (Normal_NoticeEntity notice in noticeList)
            {
                notice.Url = string.Concat("/notice/", notice.Id.ToString());
            }
            return noticeList;
        }
        /// <summary>
        /// 获取最新的公告
        /// </summary>
        /// <returns></returns>
        public Normal_NoticeEntity GetNewNotice()
        {
            QueryParam qp = new QueryParam();
            qp.PageSize = 1;
            qp.PageIndex = 1;
            qp.Orderfld = "AddDate desc ";
            BLLHelper bll = new BLLHelper();
            qp.Where = string.Concat("enabled=1 and  startdate<=", bll.GetSqlDateTime(DateTime.Now), " and enddate>= ", bll.GetSqlDateTime(DateTime.Now));
            List<Normal_NoticeEntity> noticeList = MvcWebCacheManager.GetNormal_NoticeList(qp,300);
            Normal_NoticeEntity noticeEntity = new Normal_NoticeEntity();
            if (noticeList != null && noticeList.Count > 0)
            {
                noticeEntity = noticeList[0];
            }
            return noticeEntity;
        }
       


    }

}
